package com.fanshuai.jmx.agent;

import com.fanshuai.jmx.mbean.Hello;
//import com.sun.jdmk.comm.HtmlAdaptorServer;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import java.lang.management.ManagementFactory;

public class HttpAgent {
    public static void init() throws Exception {
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();

        ObjectName adaptorName = new ObjectName("HelloAgent:name=httpAgent,port=8082");

//        HtmlAdaptorServer htmlAdaptorServer = new HtmlAdaptorServer();
//        mBeanServer.registerMBean(htmlAdaptorServer, adaptorName);
//
//        register(mBeanServer);
        display(mBeanServer);

        //htmlAdaptorServer.start();
    }

    private static void register(MBeanServer mBeanServer) throws Exception {
        Hello hello = new Hello();
        hello.setName("linfangyi");
        hello.setAge(18);

        ObjectName objectName = new ObjectName("custom:name=hello");
        mBeanServer.registerMBean(hello, objectName);
    }

    private static void display(MBeanServer mBeanServer) throws Exception {
        String name = (String) mBeanServer.getAttribute(new ObjectName("custom:name=hello"), "Name");
        System.out.println(name);
    }
}
